import request from './index.js'

/**
 * 获取社区文章分类列表
 * @returns {Promise}
 */
export function getCommunityCategories() {
  return request({
    url: '/community/categories',
    method: 'GET'
  })
}

/**
 * 创建社区文章
 * @param {Object} articleDTO 文章数据
 * @returns {Promise}
 */
export function createCommunityArticle(articleDTO) {
  return request({
    url: '/community/articles',
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
      // Authorization 自动在 request.js 拦截器里加
    },
    data: articleDTO
  })
}

/**
 * 获取社区文章详情
 * @param {number} id 文章ID
 * @returns {Promise}
 */
export function getCommunityArticleDetail(id) {
  return request({
    url: `/community/articles/${id}`,
    method: 'GET'
  })
}

/**
 * 点赞社区文章
 * @param {number} articleId 文章ID
 * @returns {Promise}
 */
export function likeCommunityArticle(articleId) {
  return request({
    url: `/community/article-info/like/${articleId}`,
    method: 'POST'
  })
}

/**
 * 取消点赞社区文章
 * @param {number} articleId 文章ID
 * @returns {Promise}
 */
export function unlikeCommunityArticle(articleId) {
  return request({
    url: `/community/article-info/like/${articleId}`,
    method: 'DELETE'
  })
}

/**
 * 分页查询社区文章列表
 * @param {Object} params { page, pageSize, categoryId }
 * @returns {Promise}
 */
export function getCommunityArticles(params) {
  return request({
    url: '/community/articles',
    method: 'GET',
    params
  })
}

/**
 * 根据分类ID获取该分类下所有文章
 * @param {Object} params { categoryId, page, pageSize }
 * @returns {Promise}
 */
export function getCommunityArticlesByCategory(params) {
  return request({
    url: `/community/articles/category/${params.categoryId}`,
    method: 'GET',
    params: {
      page: params.page,
      pageSize: params.pageSize
    }
  })
}

/**
 * 获取当前用户创建的文章
 * @param {Object} params { page, pageSize }
 * @returns {Promise}
 */
export function getMyArticles(params) {
  return request({
    url: '/community/articles/my',
    method: 'GET',
    params
  })
}

/**
 * 删除文章
 * @param {number} id 文章ID
 * @returns {Promise}
 */
export function deleteArticle(id) {
  return request({
    url: `/community/articles/${id}`,
    method: 'DELETE'
  })
}

/**
 * 更新文章
 * @param {number} id 文章ID
 * @param {Object} articleDTO 文章数据
 * @returns {Promise}
 */
export function updateArticle(id, articleDTO) {
  return request({
    url: `/community/articles/${id}`,
    method: 'PUT',
    data: articleDTO
  })
}

/**
 * 创建社区分类
 * @param {Object} category 分类对象 { name, description, sortOrder }
 * @returns {Promise}
 */
export function createCommunityCategory(category) {
  return request({
    url: '/community/categories',
    method: 'POST',
    data: category,
    headers: {
      'Content-Type': 'application/json'
    }
  })
}

/**
 * 删除社区分类
 * @param {number} categoryId 分类ID
 * @returns {Promise}
 */
export function deleteCommunityCategory(categoryId) {
  return request({
    url: `/community/categories/${categoryId}`,
    method: 'DELETE'
  })
}

/**
 * 收藏社区文章
 * @param {number} articleId 文章ID
 * @returns {Promise}
 */
export function collectCommunityArticle(articleId) {
  return request({
    url: `/community/article-info/collect/${articleId}`,
    method: 'POST',
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded',
      // Authorization 自动在 request.js 拦截器里加
    }
  })
}

/**
 * 获取当前用户收藏的文章
 * @param {Object} params { page, pageSize }
 * @returns {Promise}
 */
export function getMyCollectedArticles(params) {
  return request({
    url: '/community/articles/collected',
    method: 'GET',
    params,
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded',
      // Authorization 自动在 request.js 拦截器里加
    }
  })
}

/**
 * 检查是否已点赞文章
 * @param {number} articleId 文章ID
 * @returns {Promise}
 */
export function checkArticleLiked(articleId) {
  return request({
    url: `/community/article-info/has-liked/${articleId}`,
    method: 'GET',
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    }
  })
}

/**
 * 取消收藏社区文章
 * @param {number} articleId 文章ID
 * @returns {Promise}
 */
export function uncollectCommunityArticle(articleId) {
  return request({
    url: `/community/article-info/collect/${articleId}`,
    method: 'DELETE',
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded',
      // Authorization 自动在 request.js 拦截器里加
    }
  })
}

/**
 * 获取文章评论列表
 * @param {number} articleId 文章ID
 * @returns {Promise}
 */
export function getArticleComments(articleId) {
  return request({
    url: `/community/article-info/comments/${articleId}`,
    method: 'GET',
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
      // Authorization 自动在 request.js 拦截器里加，不需要手动添加
    }
  })
}

/**
 * 创建文章评论
 * @param {Object} commentDTO { articleId, content, parentId, replyUserId }
 * @returns {Promise}
 */
export function createArticleComment(commentDTO) {
  return request({
    url: '/community/article-info/comment',
    method: 'POST',
    headers: {
      'Content-Type': 'application/json'
      // Authorization 自动在 request.js 拦截器里加，不需要手动添加
    },
    data: commentDTO
  })
}

/**
 * 删除评论
 * @param {number} commentId 评论ID
 * @returns {Promise}
 */
export function deleteArticleComment(commentId) {
  return request({
    url: `/community/article-info/comment/${commentId}`,
    method: 'DELETE',
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded',
      // Authorization 自动在 request.js 拦截器里加
    }
  })
}

/**
 * 更新社区分类
 * @param {number} id 分类ID
 * @param {Object} category 分类对象 { name, description, sortOrder }
 * @returns {Promise}
 */
export function updateCommunityCategory(id, category) {
  return request({
    url: `/community/categories/${id}`,
    method: 'PUT',
    data: category,
    headers: {
      'Content-Type': 'application/json'
    }
  })
}